home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / 7edit / source / svaepaste.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.5 KB  |  147 lines

  1. /*
  2.     File:        SVAEPaste.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Original version by Jon Lansdell and Nigel Humphreys.
  7.                 3.1 updates by Greg Sutton.
  8.  
  9.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #include "SVAEPaste.h"
  25.  
  26. #include "SVEditAEUtils.h"
  27. #include "SVEditWindow.h"        // for DPtrFromWindowPtr()
  28.  
  29. #include "SVAESelect.h"
  30.  
  31.  
  32. #pragma segment AppleEvent
  33.  
  34. // Handle a paste from scrap e.g 'paste last word of document 1' would change the
  35. // last word in the front window to what's in the scrap.
  36. // If no reference is given then the selection of the front window
  37. // is used.
  38.      
  39. pascal OSErr    DoPaste(const AppleEvent *theAppleEvent, AppleEvent *reply, long refcon)
  40. {
  41. #pragma unused (reply, refcon)
  42.  
  43.     AEDesc        directObj = {typeNull, NULL};
  44.     TextToken    aTextToken;
  45.     short        ignore;
  46.     OSErr        err;
  47.  
  48.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
  49.     // If we get an error here it just means that they haven't supplied a reference to
  50.     // an object to paste to - so paste to the current section instead.
  51.     
  52.     if (directObj.descriptorType != typeNull)
  53.         err = PasteDesc(&directObj);
  54.     else
  55.     {            // Just paste to the selection of the front window
  56.         err = GetWindowSelection(FrontWindow(), &aTextToken, &ignore);
  57.         if (noErr != err) goto done;
  58.         
  59.         err = PasteTextToken(&aTextToken);
  60.     }
  61.  
  62. done:    
  63.     if (directObj.dataHandle)
  64.         AEDisposeDesc(&directObj);
  65.         
  66.     return(err);
  67. }
  68.  
  69.  
  70. OSErr    PasteTextToken(TextToken* theToken)
  71. {
  72.     WindowPtr        aWindow;
  73.     DPtr            docPtr;
  74.     OSErr            err;
  75.     
  76.     aWindow = theToken->tokenWindow;
  77.     docPtr = DPtrFromWindowPtr(theToken->tokenWindow);
  78.     
  79.     if (! aWindow || ! docPtr)
  80.         return(errAENoSuchObject);
  81.  
  82.                     // Set this tokens selection
  83.     err = SelectTextToken(theToken);
  84.     if (noErr != err) goto done;
  85.  
  86.     TEStylePaste(docPtr->theText);     
  87.             
  88.     docPtr->dirty = true;
  89.     AdjustScrollbars(docPtr, false);
  90.     DrawPageExtras(docPtr);
  91.             
  92. done:
  93.     return(err);
  94. }
  95.  
  96. OSErr    PasteTextDesc(AEDesc* textDesc)
  97. {
  98.     TextToken        aTextToken;
  99.     Size            actualSize;
  100.     OSErr            err;
  101.  
  102.     if (typeMyText != textDesc->descriptorType)
  103.         return(errAETypeError);
  104.         
  105.     GetRawDataFromDescriptor(textDesc, (Ptr)&aTextToken, sizeof(aTextToken), &actualSize);
  106.  
  107.     err = PasteTextToken(&aTextToken);
  108.     
  109.     return(err);
  110. }
  111.  
  112.  
  113. OSErr    PasteDesc(AEDesc* aDesc)
  114. {
  115.     AEDesc        pasteDesc = {typeNull, NULL},
  116.                 textDesc = {typeNull, NULL};
  117.     OSErr        err;
  118.     
  119.     if (typeObjectSpecifier == aDesc->descriptorType)
  120.         err = AEResolve(aDesc, kAEIDoMinimum, &pasteDesc);
  121.     else
  122.         err = AEDuplicateDesc(aDesc, &pasteDesc);
  123.         
  124.     if (noErr != err) goto done;
  125.     
  126.     switch (pasteDesc.descriptorType)
  127.     {
  128.         case typeAEList:
  129.             err = errAETypeError;
  130.             // We can't handle pasting more than one item from the scrap
  131.             break;
  132.             
  133.         default:
  134.             err = AECoerceDesc(&pasteDesc, typeMyText, &textDesc);
  135.             if (noErr != err) goto done;
  136.             err = PasteTextDesc(&textDesc);
  137.     }
  138.     
  139. done:
  140.     if (pasteDesc.dataHandle)
  141.         AEDisposeDesc(&pasteDesc);
  142.     if (textDesc.dataHandle)
  143.         AEDisposeDesc(&textDesc);
  144.     
  145.     return(err);
  146. }
  147.